package com.un.ebs.businessObject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.un.ebs.core.enums.DataTypeEnum;
import com.un.ebs.core.enums.RequestArgEnum;
import com.un.ebs.core.metadata.FunctionInfo;
import com.un.ebs.core.metadata.MetadataField;
import com.un.ebs.core.metadata.RequestArg;
import com.un.ebs.sys.domain.Column;
import com.un.ebs.sys.domain.Table;
import com.un.ebs.sys.enums.DisplayEnums;
import com.un.ebs.sys.enums.FunctionsEnums;
import com.un.ebs.sys.mapper.ColumnMapper;
import com.un.ebs.businessObject.service.RelQueryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zc
 * @ClassName RelQueryServiceImpl
 * @Description
 * @Date 2021/1/21
 */
@Service
@Slf4j
public class RelQueryServiceImpl implements RelQueryService {
    @Autowired
    private ColumnMapper columnMapper;

    @Override
    public FunctionInfo getQueryFunc(Table table) {
        FunctionInfo func = new FunctionInfo();
        String url = FunctionsEnums.QUERY.getUrl();
        String desc = FunctionsEnums.QUERY.getDesc();
        List<RequestMethod> method = Arrays.asList(FunctionsEnums.QUERY.getRequestMethod());

        func.setUrl(String.format(url, table.getId()));
        func.setDescription(desc);
        func.setMethod(method);
        func.setName(table.getTableName());

        List<RequestArg> requestArgs = new ArrayList<>();
        List<MetadataField> metadataFields = new ArrayList<>();    
        LambdaQueryWrapper<Column> qw = new LambdaQueryWrapper<Column>();
        qw.eq(Column::getTableId, table.getId());
        List<Column> columns = columnMapper.selectList(qw);
		columns = columns.stream().filter(x -> !DisplayEnums.getColumnNameList().contains(x.getColumnName()))
				.collect(Collectors.toList()); // 过滤支持相关字段
        for (Column column : columns) {
            MetadataField metadataField = new MetadataField();
            metadataField.setName(column.getColumnName());
            metadataField.setDisplayName(column.getColumnDesc());
            metadataField.setDataType(column.getColumnType());
            metadataField.setIsArray(0);
            metadataFields.add(metadataField);
        }

        List<RequestArg> request = getListRequest(requestArgs);
        List<MetadataField> response = getListResponse(metadataFields);
        func.setRequest(request);
        func.setResponse(response);
        return func;
    }


    /**
     * 构造返回体
     *
     * @param metadataFields record 内的字段描述
     * @return
     */
    private List<MetadataField> getListResponse(List<MetadataField> metadataFields) {
        //1.构造code
        MetadataField m1 = new MetadataField();
        m1.setName("code");
        m1.setIsArray(0);
        m1.setDataType(DataTypeEnum.INT);
        //2.构造message
        MetadataField m2 = new MetadataField();
        m2.setName("message");
        m2.setIsArray(0);
        m2.setDataType(DataTypeEnum.STRING);
        //3.构造data
        MetadataField m3 = new MetadataField();
        m3.setName("data");
        m3.setIsArray(0);
        m3.setDataType(DataTypeEnum.OBJECT);
        //4.构造data_children
        MetadataField list_m1 = new MetadataField();
        list_m1.setName("records");
        list_m1.setIsArray(1);
        list_m1.setDataType(DataTypeEnum.OBJECT);
        list_m1.setChildField(metadataFields);

        MetadataField list_m2 = new MetadataField();
        list_m2.setName("total");
        list_m2.setIsArray(0);
        list_m2.setDataType(DataTypeEnum.NUMBER);

        MetadataField list_m3 = new MetadataField();
        list_m3.setName("size");
        list_m3.setIsArray(0);
        list_m3.setDataType(DataTypeEnum.NUMBER);

        MetadataField list_m4 = new MetadataField();
        list_m4.setName("current");
        list_m4.setIsArray(0);
        list_m4.setDataType(DataTypeEnum.NUMBER);

        List<MetadataField> dataList = new ArrayList<>();
        dataList.add(list_m1);
        dataList.add(list_m2);
        dataList.add(list_m3);
        dataList.add(list_m4);

        m3.setChildField(dataList);

        List<MetadataField> resultList = new ArrayList<>();
        resultList.add(m1);
        resultList.add(m2);
        resultList.add(m3);
        return resultList;
    }

    /**
     * 构造请求体
     *
     * @param requestArgs 请求字段
     * @return
     */
    private List<RequestArg> getListRequest(List<RequestArg> requestArgs) {


        RequestArg r1 = new RequestArg();
        r1.setName("sortName");
        r1.setDisplayName("自定义排序字段");
        r1.setDataType(DataTypeEnum.STRING);
        r1.setIsArray(0);
        r1.setRequestType(RequestArgEnum.RequestBody);

        RequestArg r2 = new RequestArg();
        r2.setName("pageIndex");
        r2.setDisplayName("页码");
        r2.setIsArray(0);
        r2.setDataType(DataTypeEnum.INT);
        r2.setRequestType(RequestArgEnum.RequestBody);

        RequestArg r3 = new RequestArg();
        r3.setName("pageSize");
        r3.setDisplayName("条数");
        r3.setIsArray(0);
        r3.setDataType(DataTypeEnum.INT);
        r3.setRequestType(RequestArgEnum.RequestBody);

        RequestArg r4 = new RequestArg();
        r4.setName("objectName");
        r4.setDisplayName("bo名称");
        r4.setIsArray(0);
        r4.setDataType(DataTypeEnum.STRING);
        r4.setRequestType(RequestArgEnum.RequestBody);

        RequestArg r5 = new RequestArg();
        r5.setName("conditions");
        r5.setDisplayName("条件");
        r5.setIsArray(1);
        r5.setDataType(DataTypeEnum.OBJECT);
        r5.setRequestType(RequestArgEnum.RequestBody);

        List<MetadataField> list = new ArrayList<>();
        MetadataField list_m1 = new MetadataField();
        list_m1.setName("key");
        list_m1.setIsArray(0);
        list_m1.setDataType(DataTypeEnum.STRING);
        MetadataField list_m2 = new MetadataField();
        list_m2.setName("values");
        list_m2.setIsArray(1);
        list_m2.setDataType(DataTypeEnum.STRING);
        MetadataField list_m3 = new MetadataField();
        list_m3.setName("operator");
        list_m3.setIsArray(0);
        list_m3.setDataType(DataTypeEnum.STRING);

        list.add(list_m1);
        list.add(list_m2);
        list.add(list_m3);


        r5.setChildField(list);

        requestArgs.add(r1);
        requestArgs.add(r2);
        requestArgs.add(r3);
        requestArgs.add(r4);
        requestArgs.add(r5);


        return requestArgs;
    }
}
